package com.project.market.controller;

import com.project.market.db.domain.MarketIssue;
import com.project.market.service.AdminIssueService;
import com.project.market.service.impl.AdminIssueServiceImpl;
import com.project.market.util.JacksonUtil;
import com.project.market.util.ResponseUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedReader;
import java.io.IOException;
import java.util.List;
import java.util.Map;

@WebServlet("/admin/issue/*")
public class AdminIssueController extends HttpServlet {

    AdminIssueService adminIssueService = new AdminIssueServiceImpl();

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        String requestURI = req.getRequestURI();
        String op = requestURI.replace(req.getContextPath() + "/admin/issue/", "");
        if ("create".equals(op)) {
            create(req, resp);
        } else if ("update".equals(op)) {
            update(req,resp);
        } else if ("delete".equals(op)) {
            delete(req,resp);
        }

    }

    /*
    删除方法，根据id删除
     */
    private void delete(HttpServletRequest req, HttpServletResponse resp) throws IOException {

        MarketIssue marketIssue = null;

        // 获取请求体内容
        BufferedReader reader = req.getReader();
        String requestBody = reader.readLine();

        String addTime = JacksonUtil.parseString(requestBody, "addTime");
        String answer = JacksonUtil.parseString(requestBody, "answer");
        Boolean deleted = JacksonUtil.parseBoolean(requestBody, "deleted");
        Integer id = JacksonUtil.parseInteger(requestBody, "id");
        String question = JacksonUtil.parseString(requestBody, "question");
        String updateTime = JacksonUtil.parseString(requestBody, "updateTime");


        marketIssue = adminIssueService.delete(addTime,answer,deleted,id,question,updateTime);
        Object flag = null;
        if(marketIssue!=null){
            flag = ResponseUtil.ok();

        }else {
            flag = ResponseUtil.fail();
        }
        String responseBody = JacksonUtil.writeValueAsString(flag);
        resp.getWriter().println(responseBody);
    }

    /*
    更新，编辑通用问题，根据id查找通用问题，并根据 question 和 answer 修改其中内容
     */
    private void update(HttpServletRequest req, HttpServletResponse resp) throws IOException {

        // 获取请求体内容
        BufferedReader reader = req.getReader();
        String requestBody = reader.readLine();

        // 取出前端输入的参数
        String addTime = JacksonUtil.parseString(requestBody, "addTime");
        String answer = JacksonUtil.parseString(requestBody, "answer");
        String id = JacksonUtil.parseString(requestBody, "id");
        String question = JacksonUtil.parseString(requestBody, "question");
        String updateTime = JacksonUtil.parseString(requestBody, "updateTime");

        MarketIssue marketIssue = adminIssueService.update(addTime,answer,id,question,updateTime);
        Object ok = ResponseUtil.ok(marketIssue);
        String responseBody = JacksonUtil.writeValueAsString(ok);
        resp.getWriter().println(responseBody);
    }


    /*
    新增方法，获取前端参数 question 和 answer
     */
    private void create(HttpServletRequest req, HttpServletResponse resp) throws IOException {

        // 读取请求体内容
        BufferedReader reader = req.getReader();
        String requestBody = reader.readLine();

        // 获取前端传递过来的参数
        String question = JacksonUtil.parseString(requestBody, "question");
        String answer = JacksonUtil.parseString(requestBody, "answer");

        // 将返回的内容封装到响应体中
        MarketIssue marketIssue = adminIssueService.create(question, answer);
        Object ok = ResponseUtil.ok(marketIssue);
        String responseBody = JacksonUtil.writeValueAsString(ok);
        resp.getWriter().println(responseBody);
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        // 分发方法
        String requestURI = req.getRequestURI();
        String op = requestURI.replace(req.getContextPath() + "/admin/issue/", "");
        if ("list".equals(op)) {
            list(req, resp);
        }

    }

    /*
    展示所有通用问题，或根据内容查找通用问题
     */
    private void list(HttpServletRequest req, HttpServletResponse resp) throws IOException {

        // 接收前端传递过来的参数
        String pageParam = req.getParameter("page");
        String limitParam = req.getParameter("limit");
        String sort = req.getParameter("sort");
        String order = req.getParameter("order");

        // 查询操作额外多出的参数
        String question = req.getParameter("question");

        // 校验
        Integer page = null;
        Integer limit = null;
        try {
            page = Integer.parseInt(pageParam);
            limit = Integer.parseInt(limitParam);
        } catch (Exception e) {
            resp.getWriter().println(JacksonUtil.writeValueAsString(ResponseUtil.badArgument()));
            return;
        }

        // 接收service的返回值作为data
        List<MarketIssue> response = adminIssueService.list(page, limit, question, sort, order);

        // 将response里面的数据通过json字符串相应出去
        Object ok = ResponseUtil.okList(response);
        String responseBody = JacksonUtil.writeValueAsString(ok);
        resp.getWriter().println(responseBody);

    }
}
